package cn.daniellee.plugin.lm.command;

import cn.daniellee.plugin.lm.LotteryMaster;
import cn.daniellee.plugin.lm.core.LotteryCore;
import cn.daniellee.plugin.lm.menu.OverviewMenu;
import cn.daniellee.plugin.lm.model.PrizePool;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class LotteryCommand implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
		// 玩家无参数使用则打开建筑菜单
		if (strings.length == 0 && commandSender instanceof Player) {
			Player player = (Player) commandSender;
			player.openInventory(OverviewMenu.generate(player));
		} else if (strings.length != 0) {
			// 打开审核GUI
			if (strings[0].equalsIgnoreCase("r") && commandSender instanceof Player && commandSender.hasPermission("building.command.review")) {
				Player player = (Player) commandSender;



			// 进行地标申请
			} else if (strings[0].equalsIgnoreCase("add") && strings.length > 1 && commandSender.hasPermission("lottery.command.add")) {
				if (LotteryMaster.getInstance().getStorage().getPrizePools().size() < 6) {
					PrizePool prizePool = new PrizePool();
					// 赋初始值
					prizePool.setId(Double.toString(Math.random()).substring(2, 5));
					prizePool.setName(strings[1]);
					prizePool.setIcon("CHEST");
					prizePool.setPriceType(PrizePool.PRICE_TYPE_MONEY);
					prizePool.setPrizes(new ArrayList<>());
					LotteryMaster.getInstance().getStorage().addPrizePool(prizePool);
				} // else 不能创建更多的奖池
			// 重载插件
			} else if (strings[0].equalsIgnoreCase("reload") && commandSender.hasPermission("building.command.reload")) {
				LotteryMaster.getInstance().reloadConfig();
				if (LotteryMaster.getInstance().loadConfig()) {
					commandSender.sendMessage((LotteryMaster.getInstance().getPrefix() + LotteryMaster.getInstance().getConfig().getString("message.reload-success", "&eReload has been successful.")).replace("&", "§"));
				}
				// 发送帮助
			} else sendHelp(commandSender);
		} else commandSender.sendMessage((LotteryMaster.getInstance().getPrefix() + LotteryMaster.getInstance().getConfig().getString("message.player-only", "&eThe console cannot open the GUI menu.")).replace("&", "§"));
		return true;
	}

	private void sendHelp(CommandSender commandSender) {
		commandSender.sendMessage(("&m&6---&m&a--------&3 : " + LotteryMaster.getInstance().getConfig().getString("prompt-prefix", "&6WorldLimiter") + "&3 : &m&a--------&m&6---").replace("&", "§"));

		String reloadText = LotteryMaster.getInstance().getConfig().getString("help.reload-help", "Reload configuration.").replace("&", "§");
		TextComponent reloadHelp = new TextComponent("/wl reload  " + reloadText);
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/wl reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(reloadHelp);
	}

}
